<?php
/*
 * 登陆控制器
 */
header("Content-Type: text/html;charset=UTF-8");
class Login extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library('session');
	}

	function index(){ /*用户登陆*/
		$this->load->view ( 'login');
	}

	public function check_login() { /*验证用户*/
		$data_array=$_POST;
		$name = $data_array['name'];
		$pswd = $data_array['pswd'];
		$sha1pswd =sha1($pswd);
		$role = '';
		$id = '';
		$query = $this->db->get_where('user', array('user_name' => urldecode($name),'password' =>$sha1pswd ));
		$count=count($query->result_array());
		if($count == 1){ /*登陆成功*/
			foreach($query->result_array() as $rows){
				$role = $rows['role'];
				$id = $rows['id'];
			}
			$login_info = array('id' => $id ,'username' => $name,'role' => $role);
			$this->session->set_userdata($login_info);
			/*更新上次登录时间*/
			$time=date('Y-m-d H:i:s', strtotime('now'));
			$data['last_region_time']=$time;

			$this->db->where('id', $id);
			$this->db->update('user', $data);
			redirect ( 'console/index/' );
		}else{
			$data['login_fail']='1';
			$this->load->view ( 'login',$data);
		}
	}

	public function quit(){ /*安全退出*/
		$login_info = array('id' => '','username' => '','role' => '');
		$this->session->unset_userdata($login_info);
		$this->load->view ( 'login');
	}
}
?>
